import axios from 'axios'
import $store from '@/store/index'
let ajaxTimer = 0;
export function request (config) {
  // loading界面的显示
  $store.commit('showLoading');
  const install = axios.create({
    // baseURL: 'https://music-api.heheda.top/',
    // baseURL: 'http://localhost:3000',
    // 解决跨域问题
    baseURL: '/api',
    timeOut: 5000,
  })
  install.interceptors.request.use(data => {
    ajaxTimer++;
    data.headers.Authorization = window.localStorage.getItem('cookie');
    return data
  }, err => {
    return err
  })
  install.interceptors.response.use(data => {
    ajaxTimer--;
    // 关闭loading
    if (ajaxTimer == 0) $store.commit('hiddenLoading');
    return data
  }, err => {
    return err
  })
  return install(config)
}
